home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / zendisk2.zip / LST11-34.ASM < prev    next >
Assembly Source File  |  1990-02-15  |  7KB  |  244 lines

  1. ;
  2. ; *** Listing 11-34 ***
  3. ;
  4. ; Illustrates animation based on block moves.
  5. ; Animates 10 images at once.
  6. ; Not a general animation implementation, but rather an
  7. ; example of the strengths and weaknesses of block-move
  8. ; based animation.
  9. ;
  10. ; Make with LZTIME.BAT, since this program is too long to be
  11. ; handled by the precision Zen timer.
  12. ;
  13.     jmp    Skip
  14. ;
  15. DELAY    equ    0        ;set to higher values to
  16.                 ; slow down for closer
  17.                 ; observation
  18. REPETITIONS equ    500        ;# of times to move and
  19.                 ; redraw the images
  20. DISPLAY_SEGMENT    equ    0b800h    ;display memory segment
  21.                 ; in 320x200 4-color
  22.                 ; graphics mode
  23. SCREEN_WIDTH    equ    80    ;# of bytes per scan line
  24. BANK_OFFSET    equ    2000h    ;offset from the bank
  25.                 ; containing the even-
  26.                 ; numbered lines on the
  27.                 ; screen to the bank
  28.                 ; containing the odd-
  29.                 ; numbered lines
  30. ;
  31. ; Used to count down # of times images are moved.
  32. ;
  33. RepCount    dw    REPETITIONS
  34. ;
  35. ; Complete info about one image that we're animating.
  36. ;
  37. Image    struc
  38. XCoord        dw    ?    ;image X location in pixels
  39. XInc        dw    ?    ;# of pixels to increment
  40.                 ; location by in the X
  41.                 ; direction on each move
  42. YCoord        dw    ?    ;image Y location in pixels
  43. YInc        dw    ?    ;# of pixels to increment
  44.                 ; location by in the Y
  45.                 ; direction on each move
  46. Image    ends
  47. ;
  48. ; List of images to animate.
  49. ;
  50. Images    label    Image
  51.     Image    <60,4,4,4>
  52.     Image    <140,0,52,2>
  53.     Image    <220,-4,100,0>
  54.     Image    <60,4,148,-2>
  55.     Image    <140,0,4,-4>
  56.     Image    <220,-4,52,-2>
  57.     Image    <60,4,100,0>
  58.     Image    <140,0,148,2>
  59.     Image    <220,-4,4,4>
  60.     Image    <60,4,52,2>
  61. ImagesEnd    label    Image
  62. ;
  63. ; Pixel pattern for the one image this program draws,
  64. ; a 32x32 3-color square.  There's a 4-pixel-wide blank
  65. ; fringe around each image, which makes sure the image at
  66. ; the old location is erased by the drawing of the image at
  67. ; the new location.
  68. ;
  69. TheImage    label    byte
  70.     rept    4
  71.     dw    5 dup (0)    ;top blank fringe
  72.     endm
  73.     rept    32
  74.     db    00h        ;left blank fringe
  75.     dw    0ffffh, 05555h, 0aaaah, 0ffffh
  76.     db    00h        ;right blank fringe
  77.     endm
  78.     rept    4
  79.     dw    5 dup (0)    ;bottom blank fringe
  80.     endm
  81. IMAGE_HEIGHT    equ    40    ;# of rows in the image
  82.                 ; (including blank fringe)
  83. IMAGE_WIDTH    equ    10    ;# of bytes across the image
  84.                 ; (including blank fringe)
  85. ;
  86. ; Block-move draws the image of a 3-color square at the
  87. ; specified screen location. Assumes images start on
  88. ; even-numbered scan lines and are an even number of
  89. ; scan lines high. Always draws images byte-aligned in
  90. ; display memory.
  91. ;
  92. ; Input:
  93. ;    CX = X coordinate of upper left corner at which to
  94. ;        draw image (will be adjusted to nearest
  95. ;        less-than or equal-to multiple of 4 in order
  96. ;        to byte-align)
  97. ;    DX = Y coordinate of upper left corner at which to
  98. ;        draw image
  99. ;    ES = display memory segment
  100. ;
  101. ; Output: none
  102. ;
  103. ; Registers altered: AX, CX, DX, SI, DI, BP
  104. ;
  105. BlockDrawImage:
  106.     push    bx    ;preserve the main loop's pointer
  107.     shr    dx,1    ;divide the row # by 2 to compensate
  108.             ; for the 2-bank nature of 320x200
  109.             ; 4-color mode
  110.     mov    ax,SCREEN_WIDTH
  111.     mul    dx    ;start offset of top row of image in
  112.             ; display memory
  113.     shr    cx,1    ;divide the X coordinate by 4
  114.     shr    cx,1    ; because there are 4 pixels per
  115.             ; byte
  116.     add    ax,cx    ;point to the offset at which the
  117.             ; upper left byte of the image will
  118.             ; go
  119.     mov    di,ax
  120.     mov    si,offset TheImage
  121.             ;point to the start of the one image
  122.             ; we always draw
  123.     mov    ax,BANK_OFFSET-SCREEN_WIDTH+IMAGE_WIDTH
  124.             ;offset from the end of an odd line
  125.             ; of the image in display memory to
  126.             ; the start of the next even line of
  127.             ; the image
  128.     mov    bx,BANK_OFFSET-IMAGE_WIDTH
  129.             ;offset from the end of an even line
  130.             ; of the image in display memory to
  131.             ; the start of the next odd line of
  132.             ; the image
  133.     mov    dx,IMAGE_HEIGHT/2
  134.             ;# of even/odd numbered row pairs to
  135.             ;  draw in the image
  136.     mov    bp,IMAGE_WIDTH/2
  137.             ;# of words to draw per row of the
  138.             ; image. Note that IMAGE_WIDTH must
  139.             ; be an even number since we draw
  140.             ; the image a word at a time
  141. BlockDrawRowLoop:
  142.     mov    cx,bp    ;# of words to draw per row of the
  143.             ; image
  144.     rep    movsw    ;draw a whole even row with this one
  145.             ; repeated instruction
  146.     add    di,bx    ;point to the start of the next
  147.             ; (odd) row of the image, which is
  148.             ; in the second bank of display
  149.             ; memory
  150.     mov    cx,bp    ;# of words to draw per row of the
  151.             ; image
  152.     rep    movsw    ;draw a whole odd row with this one
  153.             ; repeated instruction
  154.     sub    di,ax
  155.             ;point to the start of the next
  156.             ; (even) row of the image, which is
  157.             ; in the first bank of display
  158.             ; memory
  159.     dec    dx    ;count down the row pairs
  160.     jnz    BlockDrawRowLoop
  161.     pop    bx    ;restore the main loop's pointer
  162.     ret
  163. ;
  164. ; Main animation program.
  165. ;
  166. Skip:
  167. ;
  168. ; Set the mode to 320x200 4-color graphics mode.
  169. ;
  170.     mov    ax,0004h    ;AH=0 is mode select fn
  171.                 ;AL=4 selects mode 4,
  172.                 ; 320x200 4-color mode
  173.     int    10h        ;invoke the BIOS video
  174.                 ; interrupt to set the mode
  175. ;
  176. ; Point ES to display memory for the rest of the program.
  177. ;
  178.     mov    ax,DISPLAY_SEGMENT
  179.     mov    es,ax
  180. ;
  181. ; We'll always want to count up.
  182. ;
  183.     cld
  184. ;
  185. ; Start timing.
  186. ;
  187.     call    ZTimerOn
  188. ;
  189. ; There's no need to draw all the images initially with
  190. ; block-move animation.
  191. ;
  192. ; Move and redraw each image in turn REPETITIONS times.
  193. ; Redrawing automatically erases the image at the old
  194. ; location, thanks to the blank fringe.
  195. ;
  196. MainMoveAndDrawLoop:
  197.     mov    bx,offset Images    ;list of images
  198. ImageMoveLoop:
  199.     mov    cx,[bx+XCoord]    ;X coordinate
  200.     cmp    cx,0        ;at left edge?
  201.     ja    CheckRightMargin ;no
  202.     neg    [bx+XInc]    ;yes, so bounce
  203. CheckRightMargin:
  204.     cmp    cx,280        ;at right edge?
  205.     jb    MoveX        ;no
  206.     neg    [bx+XInc]    ;yes, so bounce
  207. MoveX:
  208.     add    cx,[bx+XInc]    ;move horizontally
  209.     mov    [bx+XCoord],cx    ;save the new location
  210.     mov    dx,[bx+YCoord]    ;Y coordinate
  211.     cmp    dx,0        ;at top edge?
  212.     ja    CheckBottomMargin ;no
  213.     neg    [bx+YInc]    ;yes, so bounce
  214. CheckBottomMargin:
  215.     cmp    dx,160        ;at bottom edge?
  216.     jb    MoveY        ;no
  217.     neg    [bx+YInc]    ;yes, so bounce
  218. MoveY:
  219.     add    dx,[bx+YInc]    ;move horizontally
  220.     mov    [bx+YCoord],dx    ;save the new location
  221.     call    BlockDrawImage    ;draw the image at its
  222.                 ; new location
  223.     add    bx,size Image    ;point to the next image
  224.     cmp    bx,offset ImagesEnd
  225.     jb    ImageMoveLoop    ;move next image, if there
  226.                 ; is one
  227.  
  228. if DELAY
  229.     mov    cx,DELAY    ;slow down as specified
  230.     loop    $
  231. endif
  232.     dec    [RepCount]    ;animate again?
  233.     jnz    MainMoveAndDrawLoop ;yes
  234.     call    ZTimerOff    ;done timing
  235. ;
  236. ; Return to text mode.
  237. ;
  238.     mov    ax,0003h    ;AH=0 is mode select fn
  239.                 ;AL=3 selects mode 3,
  240.                 ; 80x25 text mode
  241.     int    10h        ;invoke the BIOS video
  242.                 ; interrupt to set the mode
  243.